package com.zch.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;

import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;

import com.base.action.CoreController;
import com.zch.dao.model.ProjectAnalyze;
import com.zch.dao.model.ProjectConnect;
import com.zch.dao.model.ProjectConnectExample;
import com.zch.dao.model.ProjectMessage;
import com.zch.dao.model.ZaitiType;
import com.zch.daoEx.model.ProjectConnectEx;
import com.zch.service.ProjectAnalyzeService;
import com.zch.service.ProjectConnectService;
import com.zch.service.ProjectMessageService;

@RequestMapping("projectConnect")
@Controller
@LoginFilter
public class ProjectConnectController extends CoreController{

	@Autowired
	private ProjectConnectService projectConnectService;
	@Autowired
    private ProjectMessageService projectMessageService;
	@Autowired
    private MessageService messageService;
    @Autowired
    private ProjectAnalyzeService projectAnalyzeService;
	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
       PaginationSupport.byPage(page, rows);
       Map<String, Object> map = new HashMap<String, Object>();
       map.put("name", name);
       List<ProjectConnectEx> list = projectConnectService.selectList(map);
       return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(),list));
    }

	@RequestMapping("/save")
	@ResponseBody
	public String save(ProjectConnect projectConnect){
		if (StringUtils.isBlank(projectConnect.getId())){
			projectConnectService.insert(projectConnect);
		}else{
			projectConnectService.updateByPrimaryKeySelective(projectConnect);
			ProjectConnect projectConnect2 = projectConnectService.selectByPrimaryKey(projectConnect.getId());
			
			ProjectMessage ex = new ProjectMessage();
			ex.setId(projectConnect2.getProjectId());
			ex.setIsEnable(1);
			projectMessageService.updateByPrimaryKeySelective(ex);
			
			projectConnect = projectConnectService.selectByPrimaryKey(projectConnect.getId());
            String projectId  =projectConnect.getProjectId();
            
            ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(projectId);
            String pushContent = "";
            if (projectConnect.getIsAgree() == 2){
                pushContent = "审核未通过!原因"+projectConnect.getNotAgreeReason();
                
                projectMessage.setNotAgreeReason(projectConnect.getNotAgreeReason());
                projectMessage.setIsEnable(1);
                projectMessage.setIsAgree(2);
                projectMessageService.updateByPrimaryKeySelective(projectMessage);
                //推送给项目发布人
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getUserId(), 1);
                //推送给项目可行性分析人
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getAnalyzeMaster(), 1);
            }
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		ProjectConnect projectConnect = projectConnectService.selectByPrimaryKey(id);
		projectConnect.setContent(projectConnect.getContent().replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
		return JSONUtils.serialize(projectConnect);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			projectConnectService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

    @RequestMapping("/update")
    @ResponseBody
    public String update(ProjectConnect projectConnect){
        this.projectConnectService.updateByPrimaryKeySelective(projectConnect);
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/saveConnectManager")
    @ResponseBody
    public String saveConnectManager(String userId, String id){
        if (StringUtils.isNotBlank(userId)){
        	int result = projectConnectService.updateConnectManager(userId,id);
        	if(result == 1){
        	    ProjectConnect projectConnect = projectConnectService.selectByPrimaryKey(id);
        	    ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(projectConnect.getProjectId());
                //推送给发布人
                messageService.findMessage(projectMessage.getTitle(), "审核通过！", projectConnect.getUserId(), 2);
                
                //推送给项目对接人员
                String pushContent2 = "您有一个项目需要项目公司注册！";
                messageService.findMessage(projectMessage.getTitle(), pushContent2, userId, 2);
        	}
        }
        return JSONUtils.serialize(new Ret(0));
    } 
}